<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><title>redis基础 | 江一行</title><meta name="author" content="江一行"><meta name="copyright" content="江一行"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="referrer" content="no-referrer"><meta name="description" content="Redis基础key-value键值对的方法存储数据，非关系型数据库 需要保证key的唯一性 数据结构String类型保存对象时，将其转为 json 字符串，作为 value 保存。会把一些不重要的数据存入：如 : “ “ { }等符号，占内存。 String类型是二进制安全的。可以包含任何数据。比如jpg图片或者序列化的对象 。 一个键最大能存储512MB 实例 redis 127.0.0.1:">
<meta property="og:type" content="article">
<meta property="og:title" content="redis基础">
<meta property="og:url" content="https://j_yh.gitee.io/%E6%95%B0%E6%8D%AE%E5%BA%93/redis%E5%9F%BA%E7%A1%80/index.html">
<meta property="og:site_name" content="江一行">
<meta property="og:description" content="Redis基础key-value键值对的方法存储数据，非关系型数据库 需要保证key的唯一性 数据结构String类型保存对象时，将其转为 json 字符串，作为 value 保存。会把一些不重要的数据存入：如 : “ “ { }等符号，占内存。 String类型是二进制安全的。可以包含任何数据。比如jpg图片或者序列化的对象 。 一个键最大能存储512MB 实例 redis 127.0.0.1:">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://j_yh.gitee.io/img/my/363b122c528f54df4a0446b6bab05515.png">
<meta property="article:published_time" content="2023-02-23T12:59:47.000Z">
<meta property="article:modified_time" content="2023-03-02T11:57:59.800Z">
<meta property="article:author" content="江一行">
<meta property="article:tag" content="redis">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://j_yh.gitee.io/img/my/363b122c528f54df4a0446b6bab05515.png"><link rel="shortcut icon" href="/img/favicon.ico"><link rel="canonical" href="https://j_yh.gitee.io/%E6%95%B0%E6%8D%AE%E5%BA%93/redis%E5%9F%BA%E7%A1%80/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'redis基础',
  isPost: true,
  isHome: false,
  isHighlightShrink: true,
  isToc: true,
  postUpdate: '2023-03-02 19:57:59'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">7</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">5</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">2</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/my/363b122c528f54df4a0446b6bab05515.png')"><nav id="nav"><span id="blog-info"><a href="/" title="江一行"><span class="site-name">江一行</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">redis基础</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-02-23T12:59:47.000Z" title="发表于 2023-02-23 20:59:47">2023-02-23</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-03-02T11:57:59.800Z" title="更新于 2023-03-02 19:57:59">2023-03-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.5k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>5分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="redis基础"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span><span class="post-meta-separator">|</span><span class="post-meta-commentcount"><i class="far fa-comments fa-fw post-meta-icon"></i><span class="post-meta-label">评论数:</span><a href="/%E6%95%B0%E6%8D%AE%E5%BA%93/redis%E5%9F%BA%E7%A1%80/#post-comment" itemprop="discussionUrl"><span class="valine-comment-count" data-xid="/%E6%95%B0%E6%8D%AE%E5%BA%93/redis%E5%9F%BA%E7%A1%80/" itemprop="commentCount"><i class="fa-solid fa-spinner fa-spin"></i></span></a></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Redis基础"><a href="#Redis基础" class="headerlink" title="Redis基础"></a>Redis基础</h1><p>key-value键值对的方法存储数据，非关系型数据库</p>
<p>需要保证key的唯一性</p>
<h2 id="数据结构"><a href="#数据结构" class="headerlink" title="数据结构"></a>数据结构</h2><h3 id="String类型"><a href="#String类型" class="headerlink" title="String类型"></a>String类型</h3><p>保存对象时，将其转为 json 字符串，作为 value 保存。会把一些不重要的数据存入：如 : “ “ { }等符号，占内存。</p>
<p>String类型是二进制安全的。可以包含任何数据。比如jpg图片或者序列化的对象 。</p>
<p>一个键最大能存储512MB</p>
<p><strong>实例</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; SET name <span class="string">&quot;张三&quot;</span></span><br><span class="line">OK</span><br><span class="line">redis 127.0.0.1:6379&gt; GET name</span><br><span class="line"><span class="string">&quot;张三&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="Hash类型"><a href="#Hash类型" class="headerlink" title="Hash类型"></a>Hash类型</h3><p>特别适合存储对象</p>
<p>可以将对象中的每个字段独立存储。就是说可以把对的属性名和属性值 两者存入 value。可以针对字段做CRUD，占用内存少。</p>
<p>每个 hash 可以存储 2<sup>32 - 1</sup> 键值对（40多亿）。</p>
<p><strong>实例</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; DEL person</span><br><span class="line">redis 127.0.0.1:6379&gt; HMSET person name <span class="string">&quot;zhangsan&quot;</span> age <span class="string">&quot;18&quot;</span></span><br><span class="line"><span class="string">&quot;OK&quot;</span></span><br><span class="line">redis 127.0.0.1:6379&gt; HGET person name</span><br><span class="line"><span class="string">&quot;zhangsan&quot;</span></span><br><span class="line">redis 127.0.0.1:6379&gt; HGET person age</span><br><span class="line"><span class="string">&quot;18&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="List类型"><a href="#List类型" class="headerlink" title="List类型"></a>List类型</h3><p>字符串列表，按照插入顺序排序。可以添加一个元素到列表的头部（左边）或者尾部（右边）</p>
<p>lpush：头插法。rpush：尾插法</p>
<p><strong>实例</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; DEL study</span><br><span class="line">redis 127.0.0.1:6379&gt; lpush study redis</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">redis 127.0.0.1:6379&gt; lpush study mongodb</span><br><span class="line">(<span class="built_in">integer</span>) 2</span><br><span class="line">redis 127.0.0.1:6379&gt; lpush study rabbitmq</span><br><span class="line">(<span class="built_in">integer</span>) 3</span><br><span class="line">redis 127.0.0.1:6379&gt; lrange study 0 10</span><br><span class="line">1) <span class="string">&quot;rabbitmq&quot;</span></span><br><span class="line">2) <span class="string">&quot;mongodb&quot;</span></span><br><span class="line">3) <span class="string">&quot;redis&quot;</span></span><br></pre></td></tr></table></figure>

<p><strong>Set类型</strong></p>
<p>Redis 的 Set 是 string 类型的无序集合。不允许重复。</p>
<p>集合是通过&#x3D;&#x3D;哈希表&#x3D;&#x3D;实现的，所以添加，删除，查找的复杂度都是 O(1)。</p>
<p><strong>sadd命令</strong></p>
<p>添加一个 string 元素到 key 对应的 set 集合中，成功返回 1，如果元素已经在集合中返回 0。</p>
<p><strong>实例</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; DEL subject</span><br><span class="line">redis 127.0.0.1:6379&gt; sadd subject redis</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">redis 127.0.0.1:6379&gt; sadd subject mongodb</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">redis 127.0.0.1:6379&gt; sadd subject rabbitmq</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">redis 127.0.0.1:6379&gt; sadd subject rabbitmq</span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br><span class="line">redis 127.0.0.1:6379&gt; smembers runoob</span><br><span class="line">1) <span class="string">&quot;redis&quot;</span></span><br><span class="line">2) <span class="string">&quot;rabbitmq&quot;</span></span><br><span class="line">3) <span class="string">&quot;mongodb&quot;</span></span><br></pre></td></tr></table></figure>

<p><strong>zset(set的有序集合)</strong></p>
<p>和 set 一样 string 类型的集合，不允许重复。</p>
<p>不同点：每个元素都会关联一个 double 类型的分数。根据分数来对集合内的成员排序。分数可以重复。</p>
<p><strong>zadd 命令</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">zadd key score member </span><br></pre></td></tr></table></figure>

<p><strong>实例</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; DEL subject</span><br><span class="line">redis 127.0.0.1:6379&gt; zadd subject 0 redis</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">redis 127.0.0.1:6379&gt; zadd subject 0 mongodb</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">redis 127.0.0.1:6379&gt; zadd subject 0 rabbitmq</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">redis 127.0.0.1:6379&gt; zadd subject 0 rabbitmq</span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br><span class="line">redis 127.0.0.1:6379&gt; ZRANGEBYSCORE subject 0 1000</span><br><span class="line">1) <span class="string">&quot;mongodb&quot;</span></span><br><span class="line">2) <span class="string">&quot;rabbitmq&quot;</span></span><br><span class="line">3) <span class="string">&quot;redis&quot;</span></span><br></pre></td></tr></table></figure>



<h2 id="缓存更新策略"><a href="#缓存更新策略" class="headerlink" title="缓存更新策略"></a>缓存更新策略</h2><ul>
<li><p>内存更新：redis自带的内存淘汰机制，设计初衷用于解决内存不够用</p>
</li>
<li><p>过期淘汰：expire设置过期时间</p>
</li>
<li><p>主动更新：主动完成数据库与缓存的同时更新</p>
</li>
</ul>
<p>一般采用主动更新</p>
<h3 id="主动更新方案"><a href="#主动更新方案" class="headerlink" title="主动更新方案"></a>主动更新方案</h3><p><strong>Cache Aside</strong>：调用者更新数据时，同时对缓存更新一致性好，实现一般</p>
<p><strong>Read&#x2F;Write Through</strong>：缓存和数据库集成为一个服务，服务保证两者一致性。对外提供API接口。调用者使用API，无需知道操作的是缓存还是数据库。一致性优秀，实现复杂，性能一般</p>
<p><strong>Write Back</strong>：缓存调用者的CRUD都针对缓存完成，由独立线程异步将缓存数据写入数据库<br>一致性差，实现复杂，性能好，</p>
<p>一般选择Cache Aside模式</p>
<h3 id="Cache-Aside-更新方案"><a href="#Cache-Aside-更新方案" class="headerlink" title="Cache Aside 更新方案"></a>Cache Aside 更新方案</h3><p>更新缓存还是删除缓存？</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">更新缓存会产生无效更新，存在较大线程安全问题</span><br><span class="line">删除缓存没有无效更新，线程安全问题较低</span><br></pre></td></tr></table></figure>

<p>先操作数据库还是缓存？</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">先操作数据库。</span><br><span class="line">如果先删除缓存，在多线程并发访问时，数据不一致的可能性较高</span><br><span class="line">先操作数据库则同样条件下几率更低</span><br></pre></td></tr></table></figure>

<h3 id="实现思路"><a href="#实现思路" class="headerlink" title="实现思路"></a>实现思路</h3><ul>
<li><p>查询数据时</p>
<figure class="highlight text"><table><tr><td class="code"><pre><span class="line">1. 查询缓存</span><br><span class="line">2. 命中则返回，未命中则查询数据库</span><br><span class="line">3. 数据库数据写入缓存</span><br><span class="line">4. 返回</span><br></pre></td></tr></table></figure>
</li>
<li><p>更新数据时</p>
<figure class="highlight text"><table><tr><td class="code"><pre><span class="line">1. 修改数据库</span><br><span class="line">2. 删除缓存</span><br><span class="line">3. 确保两者原子性</span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="缓存失效"><a href="#缓存失效" class="headerlink" title="缓存失效"></a>缓存失效</h2><h3 id="缓存穿透"><a href="#缓存穿透" class="headerlink" title="缓存穿透"></a>缓存穿透</h3><p>产生原因：客户端请求 缓存和数据库中都不存在的数据，会直接请求数据库</p>
<p>解决方案：</p>
<ol>
<li><p>缓存空对象</p>
<blockquote>
<p>对于不存在的数据也在redis中缓存，设置短的TTL。</p>
<p>实现简单，但是会消耗内存，短期内可能数据不一致。</p>
</blockquote>
</li>
<li><p>布隆过滤</p>
<blockquote>
<p>是一种过滤算法，可以在进入redis缓存前判断是否存在。</p>
<p>实现复杂，可能会误判</p>
</blockquote>
</li>
<li><p>其他</p>
<ul>
<li>数据的基础格式校验</li>
<li>用户权限校验</li>
<li>热点参数限流</li>
</ul>
</li>
</ol>
<h3 id="缓存雪崩"><a href="#缓存雪崩" class="headerlink" title="缓存雪崩"></a>缓存雪崩</h3><p>产生原因：大量缓存同一时间失效，并发请求多的话服务器压力大</p>
<p>解决方案</p>
<ol>
<li>不同key的TTL添加随机值</li>
<li>做redis集群</li>
<li>缓存业务添加降级限流</li>
<li>业务添加多级缓存</li>
</ol>
<h3 id="缓存击穿"><a href="#缓存击穿" class="headerlink" title="缓存击穿"></a>缓存击穿</h3><p>产生原因：热点key在某一时间被高并发访问，热点key突然过期，重构时间长，大量请求会直接落到数据库</p>
<p>解决方案</p>
<ol>
<li><p>添加互斥锁</p>
<blockquote>
<p>思路：缓存重建过程加锁，确保重建任务只有一个线程执行，其他线程等待。<br>分析：实现简单，无额外内存消耗，一致性好。等待导致性能降低，有死锁风险</p>
</blockquote>
</li>
<li><p>设置逻辑过期</p>
<blockquote>
<p>思路：热点key永不过期，设置一个逻辑过期时间，查询数据时通过逻辑时间判断是否重建。重建缓存需要通过互斥锁保存单线程执行，重建缓存线程异步执行。其他线程直接查询旧数据并返回<br>分析：不保证一致性，额外内存消耗，实现复杂</p>
</blockquote>
</li>
</ol>
<hr>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://j_yh.gitee.io/">江一行</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://j_yh.gitee.io/%E6%95%B0%E6%8D%AE%E5%BA%93/redis%E5%9F%BA%E7%A1%80/">https://j_yh.gitee.io/%E6%95%B0%E6%8D%AE%E5%BA%93/redis%E5%9F%BA%E7%A1%80/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://j_yh.gitee.io" target="_blank">江一行</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/redis/">redis</a></div><div class="post_share"><div class="social-share" data-image="/img/my/363b122c528f54df4a0446b6bab05515.png" data-sites="wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="next-post pull-full"><a href="/Java/Java%E5%86%85%E5%AD%98%E5%8C%BA%E5%9F%9F/" title="Java运行时数据区"><img class="cover" src="https://w.wallhaven.cc/full/j8/wallhaven-j8x7ym.jpg" onerror="onerror=null;src='/img/404.png'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Java运行时数据区</div></div></a></div></nav><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/avatar.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">江一行</div><div class="author-info__description">花有重开日，人无再少年</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">7</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">5</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">2</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/Jjiangyuhang"><i class="fab fa-github"></i><span>关注我</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/Jjiangyuhang" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="/jiang.yu.hang@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="https://space.bilibili.com/434232050" target="_blank" title="BiliBili"><i class="fa-brands fa-bilibili"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">一些学习笔记，欢迎交流讨论。</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Redis%E5%9F%BA%E7%A1%80"><span class="toc-number">1.</span> <span class="toc-text">Redis基础</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84"><span class="toc-number">1.1.</span> <span class="toc-text">数据结构</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#String%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.1.1.</span> <span class="toc-text">String类型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Hash%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.1.2.</span> <span class="toc-text">Hash类型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#List%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.1.3.</span> <span class="toc-text">List类型</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%93%E5%AD%98%E6%9B%B4%E6%96%B0%E7%AD%96%E7%95%A5"><span class="toc-number">1.2.</span> <span class="toc-text">缓存更新策略</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BB%E5%8A%A8%E6%9B%B4%E6%96%B0%E6%96%B9%E6%A1%88"><span class="toc-number">1.2.1.</span> <span class="toc-text">主动更新方案</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Cache-Aside-%E6%9B%B4%E6%96%B0%E6%96%B9%E6%A1%88"><span class="toc-number">1.2.2.</span> <span class="toc-text">Cache Aside 更新方案</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E7%8E%B0%E6%80%9D%E8%B7%AF"><span class="toc-number">1.2.3.</span> <span class="toc-text">实现思路</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%93%E5%AD%98%E5%A4%B1%E6%95%88"><span class="toc-number">1.3.</span> <span class="toc-text">缓存失效</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%93%E5%AD%98%E7%A9%BF%E9%80%8F"><span class="toc-number">1.3.1.</span> <span class="toc-text">缓存穿透</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%93%E5%AD%98%E9%9B%AA%E5%B4%A9"><span class="toc-number">1.3.2.</span> <span class="toc-text">缓存雪崩</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%93%E5%AD%98%E5%87%BB%E7%A9%BF"><span class="toc-number">1.3.3.</span> <span class="toc-text">缓存击穿</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/Java/%E7%B1%BB%E5%8A%A0%E8%BD%BD%E6%9C%BA%E5%88%B6/" title="类加载机制"><img src="/img/my/archive.jpg" onerror="this.onerror=null;this.src='/img/404.png'" alt="类加载机制"/></a><div class="content"><a class="title" href="/Java/%E7%B1%BB%E5%8A%A0%E8%BD%BD%E6%9C%BA%E5%88%B6/" title="类加载机制">类加载机制</a><time datetime="2023-03-04T02:35:24.000Z" title="发表于 2023-03-04 10:35:24">2023-03-04</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/Java/%E5%A4%9A%E7%BA%BF%E7%A8%8B/" title="多线程"><img src="/img/my/2510c39011c5be704182423e3a695e91.png" onerror="this.onerror=null;this.src='/img/404.png'" alt="多线程"/></a><div class="content"><a class="title" href="/Java/%E5%A4%9A%E7%BA%BF%E7%A8%8B/" title="多线程">多线程</a><time datetime="2023-02-28T12:55:06.000Z" title="发表于 2023-02-28 20:55:06">2023-02-28</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/Java/%E9%9B%86%E5%90%88/" title="集合"><img src="https://w.wallhaven.cc/full/lm/wallhaven-lmwg12.jpg" onerror="this.onerror=null;this.src='/img/404.png'" alt="集合"/></a><div class="content"><a class="title" href="/Java/%E9%9B%86%E5%90%88/" title="集合">集合</a><time datetime="2023-02-27T16:22:33.000Z" title="发表于 2023-02-28 00:22:33">2023-02-28</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/Java/Java%E5%9F%BA%E7%A1%80/" title="Java 基础"><img src="https://w.wallhaven.cc/full/4o/wallhaven-4oovr5.png" onerror="this.onerror=null;this.src='/img/404.png'" alt="Java 基础"/></a><div class="content"><a class="title" href="/Java/Java%E5%9F%BA%E7%A1%80/" title="Java 基础">Java 基础</a><time datetime="2023-02-27T13:30:00.000Z" title="发表于 2023-02-27 21:30:00">2023-02-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/Java/%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6/" title="垃圾回收"><img src="https://w.wallhaven.cc/full/n6/wallhaven-n6plgx.jpg" onerror="this.onerror=null;this.src='/img/404.png'" alt="垃圾回收"/></a><div class="content"><a class="title" href="/Java/%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6/" title="垃圾回收">垃圾回收</a><time datetime="2023-02-25T03:02:39.000Z" title="发表于 2023-02-25 11:02:39">2023-02-25</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2023 By 江一行</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'vnyeujKjy6AaK5O0RLSaVwC9-gzGzoHsz',
      appKey: 'P0RFxhK58YSXPxBnDRAbZgYd',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !false) {
  if (false) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>